package com.geovis.emergency.spd.biz.command.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.command.mapper.OdsZgtfwTyphoonMapper;
import com.geovis.emergency.spd.biz.command.mapper.OdsZgtfwTyphoonPointMapper;
import com.geovis.emergency.spd.biz.command.service.IOdsZgtfwTyphoonService;
import com.geovis.emergency.spd.entity.command.entity.OdsZgtfwTyphoon;

import com.geovis.emergency.spd.entity.command.entity.OdsZgtfwTyphoonPoint;
import com.geovis.emergency.spd.entity.command.pojo.vo.OdsTyphoonPointVO;
import com.geovis.emergency.spd.entity.command.pojo.vo.OdsTyphoonVO;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 中国台风网-台风信息主表 服务实现类
 * </p>
 *
 * @author Lonsho
 * @since 2022-10-25
*/

@Service
@RequiredArgsConstructor
@Slf4j
//@DS("ods")
public class OdsZgtfwTyphoonServiceImpl extends ServiceImpl<OdsZgtfwTyphoonMapper, OdsZgtfwTyphoon> implements IOdsZgtfwTyphoonService {

    private final OdsZgtfwTyphoonPointMapper odsZgtfwTyphoonPointMapper;


    public OdsZgtfwTyphoon getByTyphoonNo(String typhoonNo){
        return baseMapper.selectOne(
                Wrappers.lambdaQuery(OdsZgtfwTyphoon.class)
                        .eq(OdsZgtfwTyphoon::getTyphoonNo, typhoonNo)
                        .last("limit 1")
        );
    }
    @Override
    public OdsTyphoonVO getTyphoon(String typhoonNo) {
        OdsZgtfwTyphoon typhoon = baseMapper.selectOne(
                Wrappers.lambdaQuery(OdsZgtfwTyphoon.class)
                        .eq(OdsZgtfwTyphoon::getTyphoonNo, typhoonNo)
                        .last("limit 1")
        );
        if (ObjectUtil.isNotNull(typhoon)){
            OdsTyphoonVO typhoonVO = BeanUtil.toBean(typhoon, OdsTyphoonVO.class);
            typhoonVO.setBegin_time(typhoon.getBeginTime());
            typhoonVO.setEnd_time(typhoon.getEndTime());
            typhoonVO.setIs_current(typhoon.getIsCurrent());
            List<OdsZgtfwTyphoonPoint> odsZgtfwTyphoonPoints = odsZgtfwTyphoonPointMapper.selectList(
                    Wrappers.lambdaQuery(
                            OdsZgtfwTyphoonPoint.class
                    ).eq(OdsZgtfwTyphoonPoint::getTyphoonNo, typhoonNo)
                    .orderByAsc(OdsZgtfwTyphoonPoint::getTime)
            );
            List<OdsTyphoonPointVO> points = odsZgtfwTyphoonPoints.stream().map(point -> {
                OdsTyphoonPointVO pointVo = BeanUtil.toBean(point, OdsTyphoonPointVO.class);
                pointVo.setPointId(String.valueOf(pointVo.getId()));
                if(StrUtil.isNotBlank(point.getLatitude())) {
                    pointVo.setLatitude(Double.valueOf(point.getLatitude()));
                }
                if(StrUtil.isNotBlank(point.getLongitude())) {
                    pointVo.setLongitude(Double.valueOf(point.getLongitude()));
                }
                if(StrUtil.isNotBlank(point.getPower())) {
                    pointVo.setPower(Integer.valueOf(point.getPower()));
                }
                if(StrUtil.isNotBlank(point.getSpeed())) {
                    pointVo.setSpeed(Integer.valueOf(point.getSpeed()));
                }
                if(StrUtil.isNotBlank(point.getMoveSpeed())) {
                    pointVo.setMove_speed(Integer.valueOf(point.getMoveSpeed()));
                }
                if(StrUtil.isNotBlank(point.getPressure())) {
                    pointVo.setPressure(Integer.valueOf(point.getPressure()));
                }
                if(StrUtil.isNotBlank(point.getRadius7())) {
                    pointVo.setRadius_7(Integer.valueOf(point.getRadius7()));
                }
                if(StrUtil.isNotBlank(point.getRadius10())) {
                    pointVo.setRadius_10(Integer.valueOf(point.getRadius10()));
                }
                if(StrUtil.isNotBlank(point.getRadius12())) {
                    pointVo.setRadius_12(Integer.valueOf(point.getRadius12()));
                }
                if(StrUtil.isNotBlank(point.getRadius7Quad())){
                    pointVo.setRadius7_quad(JSONUtil.parse(point.getRadius7Quad()));
                }
                if(StrUtil.isNotBlank(point.getRadius10Quad())){
                    pointVo.setRadius10_quad(JSONUtil.parse(point.getRadius10Quad()));
                }
                if(StrUtil.isNotBlank(point.getRadius12Quad())){
                    pointVo.setRadius12_quad(JSONUtil.parse(point.getRadius12Quad()));
                }
                if(StrUtil.isNotBlank(point.getForecast())){
                    pointVo.setForecast(JSONUtil.parse(point.getForecast()));
                }
                return pointVo;
            }).collect(Collectors.toList());
            typhoonVO.setPoints(points);
            return typhoonVO;
        }
        return null;
    }
}
